package com.example.demo.nuoqi.service.impl;

import com.example.demo.nuoqi.dao.FavoritesMapper;
import com.example.demo.nuoqi.pojo.Favorites;
import com.example.demo.nuoqi.service.FavoritesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Favorites业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class FavoritesServiceImpl implements FavoritesService {

    @Autowired
    private FavoritesMapper favoritesMapper;


    /**
     * Favorites条件+分页查询
     * @param favorites 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Favorites> findPage(Favorites favorites, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(favorites);
        //执行搜索
        return new PageInfo<Favorites>(favoritesMapper.selectByExample(example));
    }

    /**
     * Favorites分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Favorites> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Favorites>(favoritesMapper.selectAll());
    }

    /**
     * Favorites条件查询
     * @param favorites
     * @return
     */
    @Override
    public List<Favorites> findList(Favorites favorites){
        //构建查询条件
        Example example = createExample(favorites);
        //根据构建的条件查询数据
        return favoritesMapper.selectByExample(example);
    }


    /**
     * Favorites构建查询对象
     * @param favorites
     * @return
     */
    public Example createExample(Favorites favorites){
        Example example=new Example(Favorites.class);
        Example.Criteria criteria = example.createCriteria();
        if(favorites!=null){
            // 
            if(!StringUtils.isEmpty(favorites.getId())){
                    criteria.andEqualTo("id",favorites.getId());
            }
            // 
            if(!StringUtils.isEmpty(favorites.getUserId())){
                    criteria.andEqualTo("userId",favorites.getUserId());
            }
            // 项目
            if(!StringUtils.isEmpty(favorites.getItemId())){
                    criteria.andEqualTo("itemId",favorites.getItemId());
            }
            // 
            if(!StringUtils.isEmpty(favorites.getCreateTime())){
                    criteria.andEqualTo("createTime",favorites.getCreateTime());
            }
            // 问题
            if(!StringUtils.isEmpty(favorites.getProbleId())){
                    criteria.andEqualTo("probleId",favorites.getProbleId());
            }
            // 简历
            if(!StringUtils.isEmpty(favorites.getResumeId())){
                    criteria.andEqualTo("resumeId",favorites.getResumeId());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        favoritesMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Favorites
     * @param favorites
     */
    @Override
    public void update(Favorites favorites){
        favoritesMapper.updateByPrimaryKey(favorites);
    }

    /**
     * 增加Favorites
     * @param favorites
     */
    @Override
    public void add(Favorites favorites){
        favoritesMapper.insert(favorites);
    }

    /**
     * 根据ID查询Favorites
     * @param id
     * @return
     */
    @Override
    public Favorites findById(Integer id){
        return  favoritesMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Favorites全部数据
     * @return
     */
    @Override
    public List<Favorites> findAll() {
        return favoritesMapper.selectAll();
    }
}
